package client.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import client.Client;

/**
 * TableFrame is the top-level GUI component (the window in which all other
 * components are held).  It shows the GameView, as well as a panel of tools
 * for interfacing with the game and a chat panel.  TableFrames are held by the
 * Client singleton.<br><br>
 * When the Client is notified that a vote is taking place, the Client will
 * output a summary of the vote to the chat text.  Players will then participate
 * in the vote by either typing /vote yes or /vote no.  This input will be processed
 * on the server side and directed to the VotingManager.
 */
public class TableFrame {
	private ChatPane chat = null;
	private Client client = null;
	private JFrame frame = null;
	private JPanel gamePanel = null;

	/**
	 * Create a new TableFrame for the given Client.
	 */
	public TableFrame(Client cl) {
		this.client = cl;
		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
				public void run () {
					createGUI();
				}
			});
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (java.lang.reflect.InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Returns the ChatPane for this TableFrame.
	 */
	public ChatPane getChatPane() {
		return chat;
	}

	public void endGame() {
		gamePanel.setEnabled(false);
		frame.dispose();
	}

	/**
	 * Creates the interface for the TableFrame.
	 */
	private void createGUI() {
		core.GameParticipantRegistry.put(client);

		frame = new JFrame("Cards");
		frame.setSize(1000,600);
		frame.getContentPane().setLayout(new BorderLayout());
		frame.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent we) {
				try {
					System.out.println("die");
					client.sendCommand(new commands.EndGameCommand("DIE!"));
				} catch (java.io.IOException e) {
					e.printStackTrace();
				}
			}
		});
		chat = new ChatPane(new AbstractAction(){
			public void actionPerformed(ActionEvent arg0) {
				client.sendMessage(chat.getMessage());
				chat.clearMessage();
			}
		});

		gamePanel = new JPanel(new BorderLayout());
		final JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, gamePanel, chat);
		frame.addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				split.setDividerLocation(500);
				frame.validate();
			}
		});

		GameView gv = new GameView(client.getGame(), client);
		JScrollPane scroll = new JScrollPane(gv.getComponent());
		gamePanel.add(scroll);
		gv.getComponent().setSize(1000,600);

		split.setDividerLocation(500);
		split.revalidate();

		frame.getContentPane().add(split, BorderLayout.CENTER);

		frame.setVisible(true);
		frame.getContentPane().validate();
		frame.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				client.endGame(null);
			}
		});
	}
}
